package com.uk.ui;

import org.jboss.weld.bean.NewBean;

import com.uk.data.entities.BashkiKomuna;
import com.uk.data.entities.Perdorimi;
import com.uk.data.entities.Qarku;
import com.uk.data.entities.Rrethi;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.Table.ColumnGenerator;

public class ColumnGeneratorUtils {

	
	public static ColumnGenerator perdorimiColumnGenerator(){
		
		return new ColumnGenerator() {
			
			@Override
			public Component generateCell(Table source, Object itemId, Object columnId) {
				final Label l = new Label();
				final Property val = source.getContainerProperty(itemId,columnId);
				if (val.getValue() != null)
					l.setValue(((Perdorimi)val.getValue()).getTag());
				else
					l.setValue("");
				
				if (val instanceof Property.ValueChangeNotifier) {
				Property.ValueChangeNotifier notifier = (Property.ValueChangeNotifier) val;
				notifier.addListener(new ValueChangeListener() {

					@Override
					public void valueChange(
							com.vaadin.data.Property.ValueChangeEvent event) {
						if (val.getValue() != null)
							l.setValue(((Perdorimi)val.getValue()).getTag());
						else
							l.setValue("");
						
							
						
					}});
				}
				return l;
			}
		};
	}
	
	public static ColumnGenerator qarkuColumnGenerator(){
		return new ColumnGenerator() {
			
			@Override
			public Component generateCell(Table source, Object itemId, Object columnId) {
				final Label l = new Label();
				System.out.println(columnId);
				final Property val = source.getContainerProperty(itemId,"bashkiKomuna");
				
				System.out.println(val);
				if (val.getValue() != null){
					System.out.println(val.getValue());
					l.setValue(((BashkiKomuna)val.getValue()).getRrethi().getQarku().getTag());	
				}
					
				
				else
					l.setValue("");
				
				if (val instanceof Property.ValueChangeNotifier) {
				Property.ValueChangeNotifier notifier = (Property.ValueChangeNotifier) val;
				notifier.addListener(new ValueChangeListener() {

					@Override
					public void valueChange(
							com.vaadin.data.Property.ValueChangeEvent event) {
						if (val.getValue() != null)
							l.setValue(((BashkiKomuna)val.getValue()).getRrethi().getQarku().getTag());
						else
							l.setValue("");
					}});
				}
				return l;
			}
		};
	}
	
	public static ColumnGenerator rrethiColumnGenerator(){
		return new ColumnGenerator() {
			
			@Override
			public Component generateCell(Table source, Object itemId, Object columnId) {
				final Label l = new Label();
				final Property val = source.getContainerProperty(itemId,"bashkiKomuna");
				if (val.getValue() != null)
					l.setValue(((BashkiKomuna)val.getValue()).getRrethi().getTag());
				else
					l.setValue("");
				
				if (val instanceof Property.ValueChangeNotifier) {
				Property.ValueChangeNotifier notifier = (Property.ValueChangeNotifier) val;
				notifier.addListener(new ValueChangeListener() {

					@Override
					public void valueChange(
							com.vaadin.data.Property.ValueChangeEvent event) {
						if (val.getValue() != null)
							l.setValue(((BashkiKomuna)val.getValue()).getRrethi().getTag());
						else
							l.setValue("");
					}});
				}
				return l;
			}
		};
	}
	
	public static ColumnGenerator bashkiKomunaColumnGenerator(){
		return new ColumnGenerator() {
			
			@Override
			public Component generateCell(Table source, Object itemId, Object columnId) {
				final Label l = new Label();
				final Property val = source.getContainerProperty(itemId,columnId);
				if (val.getValue() != null)
					l.setValue(((BashkiKomuna)val.getValue()).getTag());
				else
					l.setValue("");
				
				if (val instanceof Property.ValueChangeNotifier) {
				Property.ValueChangeNotifier notifier = (Property.ValueChangeNotifier) val;
				notifier.addListener(new ValueChangeListener() {

					@Override
					public void valueChange(
							com.vaadin.data.Property.ValueChangeEvent event) {
						if (val.getValue() != null)
							l.setValue(((BashkiKomuna)val.getValue()).getTag());
						else
							l.setValue("");
						
							
						
					}});
				}
				return l;
			}
		};
	}
	
	public static ColumnGenerator PoJoColumnGenerator(){
		return new ColumnGenerator() {
			
			@Override
			public Component generateCell(Table source, Object itemId, Object columnId) {
				final Label l = new Label();
				final Property val = source.getContainerProperty(itemId,columnId);
				if (val.getValue() != null)
				{
					System.out.println(val.getValue().toString());
					
					if ("true".equals(val.getValue().toString()))
						l.setValue("PO");	
					else if ("false".equals(val.getValue().toString()))
						l.setValue("JO");
				}
				else
					l.setValue("");
				
				if (val instanceof Property.ValueChangeNotifier) {
				Property.ValueChangeNotifier notifier = (Property.ValueChangeNotifier) val;
				notifier.addListener(new ValueChangeListener() {

					@Override
					public void valueChange(
							com.vaadin.data.Property.ValueChangeEvent event) {
						if (val.getValue() != null)
							if ("true".equals(val.getValue().toString()))
								l.setValue("PO");	
							else if ("false".equals(val.getValue().toString()))
								l.setValue("JO");
						else
							l.setValue("");
						
							
						
					}});
				}
				return l;
			}
		};
	}
	
}
